<?php
$status = array('status' => 'success', 'message' => 'no error occured.');

function open_db() 
{
    global $mysqli;
    $mysqli = new mysqli("localhost", "root", "zjzdw7980", "mydesktop");
    if (!$mysqli->connect_errno)
    {
        if (!$mysqli->query(
              'CREATE TABLE IF NOT EXISTS backgrounds(weather VARCHAR(32),
                                                      url VARCHAR(2083))'))
        {
            echo "Table creation failed: (" . $mysqli->errno . ") ". $mysqli->error;
            return false;
        }

        return true;
    }
    return false;
}

function get_background($key) 
{
    global $status, $mysqli, $weather;
    $has_db = open_db();
    $weather_bg = $_POST["weather_bg"];

    if ($has_db)
    {
        $found = false;
        $sql = "SELECT url FROM backgrounds WHERE weather='".$weather."'";
        $res = $mysqli->query($sql); 
        if ($res->num_rows>0)
        {
            $found = true;
            $row = $res->fetch_assoc();
            $bg = $row['url'];
        } 
        else
        {
            if ($weather_bg != null && $weather != null)
            {
                if (isImage($weather_bg))
                {
                    $sql = "INSERT INTO backgrounds 
                                  VALUES('".$weather."', '".$weather_bg."')";
                    if(!$mysqli->query($sql))
                    {
                        $status['status'] = 'error';
                        $status['message'] = "Table insertion failed: (" . $mysqli->errno . ") ". $mysqli->error;
                    }   
                    else 
                    {
                        $found = true;
                        $bg = $weather_bg;
                        $status['status'] = 'success';
                        $status['message'] = "${weather_bg} is a valid image.";
                    }
                }
                else
                {
                    $status['status'] = 'error';
                    $status['message'] = "${weather_bg} is not a valid image.";
                }
            }
        }
    }
    else
    {
        $fname = "./backgrounds.txt";
        $lines = file($fname);
        $default_background = $lines[0];
        foreach(array_slice($lines, 1) as $line_num => $line)
        {
            $data = explode("@", $line);
            $backgrounds[$data[0]]=$data[1];
        }

        if ($weather_bg != null && $weather != null &&
            $backgrounds[$weather] == null)
        {
            if (isImage($weather_bg))
            {
                file_put_contents($fname,
                                  $_POST["weather"]."@".$weather_bg."\n",
                                  FILE_APPEND);
                $backgrounds[$weather] = $weather_bg;
                $status['status'] = 'success';
                $status['message'] = "${weather_bg} is a valid image.";
            }
            else
            {
                $status['status'] = 'error';
                $status['message'] = "${weather_bg} is not a valid image.";
            }
        }
        $bg = $backgrounds[$key];
        $found = isset($bg);
    }

    return array($found? $bg : $default_background, $found);
}

function isImage($url)
{
    return is_array(getimagesize($url));
}

?>
